package com.ks.app.admin.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jdom.input.SAXBuilder;

import com.ks.app.admin.hibernate.KsMerchantRegist;
import com.ks.app.admin.service.MerchantService;


@ParentPackage("struts-default")
@Namespace("")
@InterceptorRef("defaultStack")
@Results( { @Result(name = "top", location = "/WEB-INF/admin/top.jsp"),
			@Result(name = "center", location = "/WEB-INF/admin/center.html"),
			@Result(name = "main", location = "/WEB-INF/admin/loginindex.html"),
			@Result(name = "left", location = "/WEB-INF/admin/left.jsp"),
			@Result(name = "down", location = "/WEB-INF/admin/down.html"),
			@Result(name = "middel", location = "/WEB-INF/admin/middel.html"),
			@Result(name = "index", location = "/WEB-INF/admin/index.html")
})
public class FramesetAction extends BaseAction {
	
	private static final long serialVersionUID = -5837947346925015047L;

	private String MenuRes;
	
	private KsMerchantRegist sessuser;
	
	@Resource
	private MerchantService merchantRgService;
	
	
	public KsMerchantRegist getSessuser() {
		return sessuser;
	}

	public void setSessuser(KsMerchantRegist sessuser) {
		this.sessuser = sessuser;
	}

	public String getMenuRes() {
		return MenuRes;
	}

	public void setMenuRes(String menuRes) {
		MenuRes = menuRes;
	}
	
	/**
	 * 主页面
	 * @return
	 */
	@Action("/admin/frame/login")
	public String login() {
	
		String tag = request.getParameter("tag");
		if (tag.equals("top")) {
			System.out.println("show top screen");
			return "top";
		} else if (tag.equals("down")) {
			System.out.println("show down screen");
			return "down";
		} else if (tag.equals("center")) {
			System.out.println("show center screen");
			return "center";
		}else if (tag.equals("main")) {
			System.out.println("show main screen");
			return "main";
		}else if (tag.equals("left")) {
			System.out.println("show left screen");
//			List<CnTControlParam> paramList = controlParamService.getAll();
//			request.setAttribute( "ccc", "abbb" );
			if(request.getSession().getAttribute("merRegist") !=null)
			{		
				//处理菜单
				//sessuser=(KsMerchantRegist)merchantRgService.GetKsMer(request.getSession().getAttribute("kerRegist").toString(),"").get(0);	
				//String ugroup=sessuser.getKsUserGroup();
				MenuRes=ReadXmlFileByUserGroup("3");						
				return "left";
			}		
			
			return "left";
		}else if(tag.equals("middel")){
			return "middel";
		}else if(tag.equals("index")){
			return "index";
		}

		return "";
	}
	
	@SuppressWarnings("unchecked")
	private String ReadXmlFileByUserGroup(String UserGroup)
	{
		if(UserGroup!=null&&UserGroup.trim().length()>0)
		{
			String fname="";
			//System.out.print("UserGroup:"+UserGroup);
			if(UserGroup.equals("1"))
			{		
				fname="usermenu1.xml";
			}
			else if(UserGroup.equals("2"))
			{fname="usermenu2.xml";}
			else if(UserGroup.equals("3"))
			{fname="usermenu3.xml";}
			else if(UserGroup.equals("4"))
			{fname="usermenu4.xml";}
			StringBuilder reslist=new StringBuilder();
			 try {    
				    SAXBuilder builder = new SAXBuilder();    
				    org.jdom.Document doc = builder.build(new File( ServletActionContext.getServletContext().getRealPath("/") +fname));    
				    org.jdom.Element foo = doc.getRootElement();    
				    List allChildren = foo.getChildren();    
				   for (int i = 0; i < allChildren.size(); i++) {    
				     reslist.append(((org.jdom.Element) allChildren.get(i)).getChild("NO").getText()+"@");
				    }    
				   } 
			 	catch (Exception e)
			 	{				 
				    e.printStackTrace();  
				    return "";
				   }
			return reslist.toString(); 
		}
		return "";		
	}   	
	
	
}
